#include <nxos.h>

NX_PRIVATE NX_Error test_map(void)
{
    NX_Solt shmaSolt = NX_SOLT_INVALID_VALUE;
    NX_Solt shm = NX_ShareMemOpen("shm0", 4096, 0); 
    if (shm < 0)
    {
        NX_Printf("create shm error\n");
        return NX_ERROR;
    }
    void * shmAddr = NX_ShareMemMap(shm, &shmaSolt);
    if (shmAddr == NX_NULL)
    {
        NX_Printf("map shm error\n");
        NX_ShareMemClose(shm);
        return NX_ERROR;
    }
    NX_MemZero(shmAddr, 4096);
    NX_ShareMemUnmap(shmaSolt);
    NX_ShareMemClose(shm);
    return NX_EOK;
}

NX_PRIVATE NX_Error test_map2(void)
{
    NX_Solt shmaSolt = NX_SOLT_INVALID_VALUE;
    NX_Solt shmaSolt2 = NX_SOLT_INVALID_VALUE;
    NX_Solt shm = NX_ShareMemOpen("shm0", 32 * 1024, 0); 
    if (shm < 0)
    {
        NX_Printf("create shm error\n");
        return NX_ERROR;
    }
    void * shmAddr = NX_ShareMemMap(shm, &shmaSolt);
    if (shmAddr == NX_NULL)
    {
        NX_Printf("map shm error\n");
        NX_ShareMemClose(shm);
        return NX_ERROR;
    }
    NX_MemZero(shmAddr, 32 * 1024);

    void * shmAddr2 = NX_ShareMemMap(shm, &shmaSolt2);
    if (shmAddr2 == NX_NULL)
    {
        NX_Printf("map shm error\n");
        NX_ShareMemUnmap(shmaSolt);
        NX_ShareMemClose(shm);
        return NX_ERROR;
    }
    NX_MemZero(shmAddr2, 32 * 1024);

    NX_ShareMemUnmap(shmaSolt2);
    NX_ShareMemUnmap(shmaSolt);
    NX_ShareMemClose(shm);
    return NX_EOK;
}

NX_PRIVATE NX_Error test_open(void)
{
    NX_Solt shm = NX_ShareMemOpen("shm0", 4096, 0); 
    if (shm < 0)
    {
        NX_Printf("create shm error\n");
        return NX_ERROR;
    }
    NX_Printf("open shm0 ok.");
    NX_Solt shm2 = NX_ShareMemOpen("shm0", 4096, 0); 
    if (shm2 < 0)
    {
        NX_Printf("create shm error don't want\n");
        return NX_ERROR;
    }
    NX_Printf("open shm0 ok.");

    NX_ShareMemClose(shm2);
    NX_ShareMemClose(shm);
    return NX_EOK;
}

NX_PRIVATE NX_Error test_open_new(void)
{
    NX_Solt shm = NX_ShareMemOpen("shm0", 4096, NX_SHAREMEM_CREATE_NEW); 
    if (shm < 0)
    {
        NX_Printf("create shm error\n");
        return NX_ERROR;
    }

    NX_Solt shm2 = NX_ShareMemOpen("shm0", 4096, NX_SHAREMEM_CREATE_NEW); 
    if (shm2 < 0)
    {
        NX_Printf("create shm error, wan't it\n");    
        NX_ShareMemClose(shm);
        return NX_ERROR;
    }

    NX_ShareMemClose(shm2);
    NX_ShareMemClose(shm);
    return NX_EOK;
}

NX_PRIVATE NX_Error test_unmap(void)
{
    NX_Solt shmaSolt = NX_SOLT_INVALID_VALUE;
    NX_Solt shm = NX_ShareMemOpen("shm0", 4096, 0); 
    if (shm < 0)
    {
        NX_Printf("create shm error\n");
        return NX_ERROR;
    }
    void * shmAddr = NX_ShareMemMap(shm, &shmaSolt);
    if (shmAddr == NX_NULL)
    {
        NX_Printf("map shm error\n");
        NX_ShareMemClose(shm);
        return NX_ERROR;
    }
    NX_MemZero(shmAddr, 4096);

    NX_Printf("can't mem unmap share memory, result %s\n", NX_ErrorToString(NX_MemUnmap(shmAddr, 4096)));

    NX_ShareMemUnmap(shmaSolt);
    NX_ShareMemClose(shm);
    return NX_EOK;
}

NX_Error TEST_Shm(char *cmdline)
{
    NX_Printf("test test_map\n");
    test_map();
    NX_Printf("test test_map ok\n");
    NX_Printf("test test_map2\n");
    test_map2();
    NX_Printf("test test_map2 ok\n");
    NX_Printf("test test_open\n");
    test_open();
    NX_Printf("test test_open ok\n");
    NX_Printf("test test_open_new\n");
    test_open_new();
    NX_Printf("test test_open_new ok\n");
    NX_Printf("test test_unmap\n");
    test_unmap();
    NX_Printf("test test_unmap ok\n");
    
    return NX_EOK;
}
